package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysStoreMapper;
import com.ruoyi.system.domain.SysStore;
import com.ruoyi.system.service.ISysStoreService;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.system.service.ISysConfigService;

/**
 * 门店Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-26
 */
@Service
public class SysStoreServiceImpl implements ISysStoreService 
{
    @Autowired
    private SysStoreMapper sysStoreMapper;

    @Autowired
    private ISysConfigService configService;

    /**
     * 查询门店
     * 
     * @param storeId 门店主键
     * @return 门店
     */
    @Override
    public SysStore selectSysStoreByStoreId(Long storeId)
    {
        return sysStoreMapper.selectSysStoreByStoreId(storeId);
    }

    /**
     * 查询门店列表
     * 
     * @param sysStore 门店
     * @return 门店
     */
    @Override
    public List<SysStore> selectSysStoreList(SysStore sysStore)
    {
        return sysStoreMapper.selectSysStoreList(sysStore);
    }
    /**
     * 查询教室管理树
     *
     * @param store 教室信息
     * @return 所有教室信息
     */
    @Override
    public List<Ztree> selectStoreTree(SysStore store)
    {
        List<SysStore> storeList = sysStoreMapper.selectSysStoreList(store);
        List<Ztree> ztrees = initZtree(storeList);
        return ztrees;
    }


    /**
     * 对象转教室树
     *
     * @param storeList 教室列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<SysStore> storeList)
    {
        return initZtree(storeList, null);
    }

    /**
     * 对象转教室树
     *
     * @param storeList 教室列表
     * @param roleStoreList 角色已存在菜单列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<SysStore> storeList, List<String> roleStoreList)
    {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        boolean isCheck = StringUtils.isNotNull(roleStoreList);
        String storeName = configService.selectConfigByKey("sys.config.companyName");
        Ztree ztree = new Ztree();
        ztree.setpId(0L);
        ztree.setId(100L);
        ztree.setName(storeName);
        ztree.setTitle(storeName);
        ztrees.add(ztree);

        HashMap<String, Long> citys = new HashMap<String, Long>();
        List<SysDictData> dictData = DictUtils.getDictCache("sys_city_list");

        Long cityId = 100L;
        for (SysDictData city : dictData) {
            ztree = new Ztree();
            ztree.setpId(100L);
            cityId = cityId + 1;
            citys.put(city.getDictLabel(), cityId);
            ztree.setId(cityId);
            ztree.setName(city.getDictLabel());
            ztree.setTitle(city.getDictLabel());
            ztrees.add(ztree);
        }
        return ztrees;
    }
    /**
     * 新增门店
     * 
     * @param sysStore 门店
     * @return 结果
     */
    @Override
    public int insertSysStore(SysStore sysStore)
    {
        sysStore.setCreateTime(DateUtils.getNowDate());
        return sysStoreMapper.insertSysStore(sysStore);
    }

    /**
     * 修改门店
     * 
     * @param sysStore 门店
     * @return 结果
     */
    @Override
    public int updateSysStore(SysStore sysStore)
    {
        sysStore.setUpdateTime(DateUtils.getNowDate());
        return sysStoreMapper.updateSysStore(sysStore);
    }

    /**
     * 批量删除门店
     * 
     * @param storeIds 需要删除的门店主键
     * @return 结果
     */
    @Override
    public int deleteSysStoreByStoreIds(String storeIds)
    {
        return sysStoreMapper.deleteSysStoreByStoreIds(Convert.toStrArray(storeIds));
    }

    /**
     * 删除门店信息
     * 
     * @param storeId 门店主键
     * @return 结果
     */
    @Override
    public int deleteSysStoreByStoreId(Long storeId)
    {
        return sysStoreMapper.deleteSysStoreByStoreId(storeId);
    }
}
